package itheima03;

import org.junit.Test;
import utils.JDBCUtil;

import java.sql.*;

/*
    PreparedStatement完成增删改
    步骤:
        1.获取连接Connection对象
        2.定义sql语句,参数使用?代替
        3.Connection对象获取执行sql语句的PreparedStatement对象,传递sql语句
        4.PreparedStatement对象调用方法,给sql语句中的?赋值
        5.PreparedStatement对象执行增删改查,获取结果
        6.处理结果
        7.关闭资源

        自己练习: 删除一条记录
 */
/*public class Demo03PreparedStatement {
    //添加一条数据
    @Test
    public void addOne() throws Exception {
        //1.获取连接Connection对象
        Connection con = JDBCUtil.getConnection();

        //2.定义sql语句,参数使用?代替
        String sql = "insert into users(uid,uname,upass) values(?,?,?)";

        //3.Connection对象获取执行sql语句的PreparedStatement对象,传递sql语句
        PreparedStatement pstmt = con.prepareStatement(sql);

        //4.PreparedStatement对象调用方法,给sql语句中的?赋值
        pstmt.setObject(1, "u005");
        pstmt.setObject(2, "baoqiang");
        pstmt.setObject(3, "baoqiang");

        //5.PreparedStatement对象执行增删改查,获取结果
        int result = pstmt.executeUpdate();
        //1
        System.out.println(result);
        //6.处理结果
        if (result > 0) {
            System.out.println("添加一条记录成功~~~");
        } else {
            System.out.println("添加一条记录失败~~~");
        }
        //7.关闭资源
        JDBCUtil.release(con, pstmt, null);
    }

    //修改一条数据
    @Test
    public void updateOne() throws Exception {
        //1.获取连接Connection对象
        Connection con = JDBCUtil.getConnection();

        //2.定义sql语句,参数使用?代替
        String sql = "update users set upass=? where uid=?";

        //3.Connection对象获取执行sql语句的PreparedStatement对象,传递sql语句
        PreparedStatement pstmt = con.prepareStatement(sql);

        //4.PreparedStatement对象调用方法,给sql语句中的?赋值
        pstmt.setString(1, "qiangbao");
        pstmt.setString(2, "u005");
        //5.PreparedStatement对象执行增删改查,获取结果
        int result = pstmt.executeUpdate();

        //6.处理结果
        if (result > 0) {
            System.out.println("修改一条记录成功~~~");
        } else {
            System.out.println("修改一条记录失败~~~");
        }
        //7.关闭资源
        JDBCUtil.release(con, pstmt, null);
    }
}*/


public  class  Demo03PreparedStatement {
    @Test
    public void  addone() throws SQLException {
        //1.获取连接Connection对象
        Connection connection=JDBCUtil.getConnection();
        /*Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery();*/

        //2.定义sql语句,参数使用?代替
        //3.Connection对象获取执行sql语句的PreparedStatement对象,传递sql语句
        String sql="insert into users(uid,uname,upass) values(?,?,?)";
        /*ResultSet resultSet = statement.executeQuery(sql);*/
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4.PreparedStatement对象调用方法,给sql语句中的?赋值
        preparedStatement.setObject(1,"u006");
        preparedStatement.setObject(2,"wkr");
        preparedStatement.setObject(3,"wkr");
        int i = preparedStatement.executeUpdate();
        //5.PreparedStatement对象执行增删改查,获取结果
        System.out.println(i);
       if(i>0){
           System.out.println("保存成功");
       }else {
           System.out.println("保存失败");
       }
        //6.处理结果
        //7.关闭资源
        JDBCUtil.release(connection,preparedStatement,null);
    }
    @Test
    public  void updateone() throws SQLException {
        //1.获取连接Connection对象
        Connection connection=JDBCUtil.getConnection();
        //2.定义sql语句,参数使用?代替
        String sql="update users set uname=?,upass=? where uid=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //3.Connection对象获取执行sql语句的PreparedStatement对象,传递sql语句
        //4.PreparedStatement对象调用方法,给sql语句中的?赋值
        preparedStatement.setObject(1,"111");
        preparedStatement.setObject(2,"222");
        preparedStatement.setObject(3,"u006");
        System.out.println(sql);
        int i = preparedStatement.executeUpdate();
        //5.PreparedStatement对象执行增删改查,获取结果
        if(i>0){
            System.out.println("执行成功");
        }else {
            System.out.println("执行失败");
        }
        //6.处理结果
        //7.关闭资源
        JDBCUtil.release(connection,preparedStatement,null);
    }
}